package cn.hfatec.shms.web.members.cardType.service;

import cn.hfatec.Page;
import cn.hfatec.Table;
import cn.hfatec.base.BaseDao;
import cn.hfatec.shms.system.user.model.User;
import cn.hfatec.shms.web.members.cardType.dao.CardTypeDao;
import cn.hfatec.shms.web.members.cardType.model.CardType;
import cn.hfatec.shms.web.members.cardType.model.CardTypeParams;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;


/**
 *
 * @author hfatec
 * @version 1.0
 *
 * the JAVA code is generate by middlegen
 *
 */
@Service("CardTypeService")
public class CardTypeService {

    public static final String BEAN_ID = "CardTypeService";

    //注入Dao层
    @Resource(name = "CardTypeDao")
    private CardTypeDao cardTypeDao;

    //注入BaseDao
    @Resource(name = "BaseDao")
    private BaseDao baseDao;

    /**
     *
     * 查询数据
     *
     * @param cardTypeParams
     * @param page
     *
     */
    @Transactional
    public Table queryCardTypeTable(CardTypeParams cardTypeParams, Page page){

        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("params", cardTypeParams);
        return baseDao.queryTableByPage("queryCardType", params, page);
    }

    /**
     *
     * 插入数据
     * @param cardType
     *
     */
    @Transactional
    public void saveCardType(CardType cardType,User user) {
        if(cardType.getId() != null) {
            CardType result = getCardType(cardType.getId());
            if(result != null) {
                cardTypeDao.update(cardType);
            } else {
                cardType.setCreateTime(new Date());
                cardType.setCreateUser(user.getLoginName());
                cardTypeDao.insert(cardType);
            }
        } else {
            cardType.setCreateTime(new Date());
            cardType.setCreateUser(user.getLoginName());
            cardTypeDao.insert(cardType);
        }
    }

    /**
     *
     * 删除数据
     * @param id
     *
     */
    @Transactional
    public void deleteCardType(Integer id) {

        cardTypeDao.delete(id);
    }

    /**
     *
     * 删除数据
     * @param cardTypeList
     *
     */
    @Transactional
    public void deleteCardTypeBatch(List<String> cardTypeList) {
        for(String id : cardTypeList) {
            deleteCardType(Integer.valueOf(id));
        }
    }

    /**
     *
     * 根据ID获取数据
     * @param id
     *
     */
    @Transactional
    public CardType getCardType(Integer id) {

        return cardTypeDao.getCardTypeById(id);
    }
}